<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userInfo" :model="userInfo" label-width="120px" style="margin-left: 120px; margin-top:30px" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="setuserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <component :is="usercomponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="jopcomponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDetailInfoById } from '@/api/user'
import { setuserInfoById } from '@/api/employees'
// 组件
import UserDetail from './components/user-detail'
import JopDetail from './components/jop-detail'
export default {
  components: {
    UserDetail,
    JopDetail
  },
  data() {
    return {
      id: this.$route.params.id,
      usercomponent: 'UserDetail',
      jopcomponent: 'JopDetail',
      userInfo: {
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { min: 2, max: 6, trigger: 'blur', message: '用户名长度必须2-6位字符' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$', trigger: 'blur', message: '密码必须含有数字字母,6-10位' }
        ]
      }
    }
  },
  created() {
    this.getuserInfo()
  },
  methods: {
    async getuserInfo() {
      this.userInfo = await getDetailInfoById(this.id)
    },
    async setuserInfo() {
      try {
        await this.$refs.userInfo.validate()
        this.userInfo.password = this.userInfo.password2
        await setuserInfoById(this.userInfo)
        this.$message.success('更新成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
